package com.liu.voluntary.controller;

import com.liu.voluntary.mapper.AdminMapper;
import com.liu.voluntary.pojo.Admin;
import com.liu.voluntary.service.AdminService;
import com.liu.voluntary.service.UserService;
import com.liu.voluntary.util.MD5Util;
import com.liu.voluntary.vo.RespBean;
import com.liu.voluntary.vo.RespBeanEnum;
import com.liu.voluntary.mapper.UserMapper;
import com.liu.voluntary.pojo.User;
import com.liu.voluntary.util.ValidatorUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class RegisterController {

    @Resource
    private UserMapper userMapper;
    @Resource
    private UserService userService;
    @Resource
    private AdminMapper adminMapper;
    @Resource
    private AdminService adminService;

    @RequestMapping("/registerUser")
    public String register(@RequestParam("r_mobile") String mobile,
                           @RequestParam("r_password") String password,
                           @RequestParam("r_nickname") String nickname,
                           HttpServletRequest request, HttpServletResponse response,

                           Model model) {

        RespBean registerBean = userService.register(mobile, password, request, response);
        if (registerBean.getCode() != 200){
            model.addAttribute("msg",registerBean.getMessage());
        }


        User user = new User();
        user.setMobile(mobile);
        user.setNickname(nickname);


        String dbPass = MD5Util.inputPassToDBPass(password);

        user.setPassword(dbPass);

        int i = userMapper.insert(user);
        if (i > 0) {
            model.addAttribute("msg", RespBeanEnum.REGISTER_SUCCESS.getMessage());
        }


        return "login";


    }



    @RequestMapping("/adminRegisterUser")
    public String adminRegister(@RequestParam("r_mobile") String mobile,
                           @RequestParam("r_password") String password,
                           @RequestParam("r_nickname") String nickname,
                           HttpServletRequest request, HttpServletResponse response,

                           Model model) {

        RespBean registerBean = userService.register(mobile, password, request, response);
        if (registerBean.getCode() != 200){
            model.addAttribute("msg",registerBean.getMessage());
        }


        User user = new User();
        user.setMobile(mobile);
        user.setNickname(nickname);


        String dbPass = MD5Util.inputPassToDBPass(password);

        user.setPassword(dbPass);

        int i = userMapper.insert(user);
        if (i > 0) {
            model.addAttribute("msg", RespBeanEnum.REGISTER_SUCCESS.getMessage());
        }


        return "superAdmin";


    }




    @RequestMapping("/addPage")
    public String register() {


        return "adminRegister";
    }


    @RequestMapping("/registerAdmin")
    public String toAddAdmin(@RequestParam("r_mobile") String mobile,
                             @RequestParam("r_password") String password,
                             @RequestParam("r_nickname") String nickname,
                             HttpServletRequest request,
                             HttpServletResponse response,
                             Model model) {


        RespBean registerBean = adminService.register(mobile, password, request, response);
        if (registerBean.getCode() != 200){
            model.addAttribute("msg",registerBean.getMessage());
            return "adminRegister";
        }



        //注册管理员身份的时候同时也给他一个用户的身份

        Admin admin = new Admin();
        admin.setNickname(nickname);
        admin.setMobile(mobile);
        String dbPass = MD5Util.inputPassToDBPass(password);
        admin.setPassword(dbPass);
        adminMapper.insert(admin);


        User user = new User();
        user.setNickname(nickname);
        user.setMobile(mobile);
        user.setPassword(dbPass);
        userMapper.insert(user);


        model.addAttribute("msg","注册成功");

        return "superAdmin";
    }


}
